/**
 * Created by leman on 2017/7/18.
 */
'use strict';
salesCtrl.controller('paymentCtrl', ['$rootScope', '$scope', '$http', '$location', '$stateParams', function ($rootScope, $scope, $http, $location ,$stateParams) {
    //配置
    $scope.count = 1; //数据总数
    $scope.p_pernum = 10; //每页显示条数
    //变量
    $scope.p_current = 1;  //当前页数
    $scope.p_all_page = 0;  //总页数
    $scope.pages = [];
    //初始化第一页
    getPage($scope.p_current,$scope.p_pernum,function(){
        //alert("我是第一次加载");
    });
    function getPage(page,size,callback){
        // "xxx.html?status=0&page="+page+"&size="+size
        $http.post("payable/payment-list",{
            "id":$stateParams.data
        }).success(function(res){
            // var thisData= res.data.concat().splice(page*10-10, 10);//当前页数*每页显示条数-每页显示条数,每页显示条数
            /*     if(res&&res.status==1){ */
            // console.log(res.data.bill.invoiceStatus)
            var thisData= res.data;
            $scope.invoiceDate=thisData.bill.invoiceDate==null ? '' : thisData.bill.invoiceDate.substring(0,11);//发票日期
            $scope.billDate=thisData.bill.billDate==null ? '' : thisData.bill.billDate.substring(0,11);//账单日期
            $scope.invoiceStatus =thisData.bill.invoiceStatus=='10'?'不开票': thisData.bill.invoiceStatus=='20'?'待收票' :  thisData.bill.invoiceStatus=='30'?'已收票':'';
            // $scope.count=res.data.page.totalRecord;
            $scope.data=thisData;


            reloadPno();//第一次调用并加载
            callback();
            /* }else{
             alert("加载失败");
             }  */
        });
    }
    //单选按钮选中
    $scope.select= function(id){
        //alert(id);
    };
    //首页
    $scope.p_index = function(){
        $scope.load_page(1);
    };
    //尾页
    $scope.p_last = function(){
        $scope.load_page($scope.p_all_page);
    };
    //加载某一页
    $scope.load_page = function(page){
        getPage(page,$scope.p_pernum,function(){});
    };
    //上一页
    $scope.prev_page=function(e){
        if(e.p_current==0){
            return;
        }
        getPage(e.p_current-1,$scope.p_pernum,function(){});
    };
    //下一页
    $scope.next_page=function(e){
        if(e.p_current==$scope.p_all_page){
            return;
        }
        getPage(e.p_current+1,$scope.p_pernum,function(){});
    };
    //跳转到指定页
    $scope.goBtn =function(){
        var idx=$("#on").val();
        getPage(idx,$scope.p_pernum,function(){});
    };
    //初始化页码
    var reloadPno = function(){
        $scope.pages=calculateIndexes($scope.p_current,$scope.p_all_page,8);
    };
    var calculateIndexes = function (current, length, displayLength) {
        var indexes = [];
        var start = Math.round(current - displayLength / 2);
        var end = Math.round(current + displayLength / 2);
        if (start <= 1) {
            start = 1;
            end = start + displayLength - 1;
            if (end >= length - 1) {
                end = length - 1;
            }
        }
        if (end >= length - 1) {
            end = length;
            start = end - displayLength + 1;
            if (start <= 1) {
                start = 1;
            }
        }
        for (var i = start; i <= end; i++) {
            indexes.push(i);
        }
        return indexes;
    };
    //获取时间
    $scope.getTime=function($event,str){
        layui.laydate({
            elem:$event.target,
            istime:false,//是否开启时间选择
            istoday:false,//是否显示今天
            isclear: true,//是否显示清空
            format:'YYYY-MM-DD',//日期格式
            choose:function(date){
                $scope[str]=date;
            }
        })
    }

}]);



